<?php
/**
 * @author chen
 * class RegisterController.php
 * Created on 2022/3/2 15:46
 */

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\Finance\Wallet;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Common\SmsServer;
use Illuminate\Support\Facades\Cache;
use App\Common\CommonModel;
use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class RegisterController extends Controller
{

    public function register(Request $request){

        $data = $request->post();
        $data = $this->decryptData($data);
        $rule = [
            'username'=>'required|email',
            'password'=>'required',
            'password_confirmation'=>'required',
            'smsverify'=>'required',
            'ref' => 'required',
            'time' => 'required'
        ];

        $message = [
            'username.required' => '用戶賬號不能爲空',
            'username.email' => '輸入的賬號不是郵箱信息',
            'password.required'=>'密碼不能爲空',
            'password_confirmation.required'=>'確認密碼不能爲空',
            'smsverify.required'=>'驗證碼不能爲空',
            'ref.required'=>'邀請碼不能爲空',
            'time.required'=>'時間戳不能爲空'
        ];

        $validator = Validator::make($data,$rule,$message);
        if($validator->passes()){

            $id = head(Hashids::decode($data['ref'])) - 100000;
            $refId = User::where('id',$id)->first();
            if(empty($refId)){
                return $this->ajaxError('邀請碼不存在，請聯系客服或者邀請人');
            }
            $password = $data['password'];
            if($data['password_confirmation'] != $password){
                return $this->ajaxError('兩次密碼不一致請從新填寫');
            }
            $code = Cache::store('redis')->get('email'.$data['username']);
            if($code != $data['smsverify']){
                return $this->ajaxError('郵箱驗證碼錯誤');
            }

            $userInfo = User::where('username',$data['username'])->first();
            if($userInfo){
                return $this->ajaxError('郵箱已经存在');
            }

            DB::beginTransaction();
            try {

                $attributes = [
                    'nickname' => '用户'.mt_rand(10000000,99999999),
                    'username' => $data['username'],
                    'password' => password_hash($password,PASSWORD_DEFAULT),
                    'secondary_password' =>  password_hash($password,PASSWORD_DEFAULT),
                    'type'=>1
                ];

                //关联模型中的parent_id方法
                $attributes['parent_id'] = $refId;
                $user = User::create($attributes);
//                $partnerRoute = CommonModel::getInstance();
//                $partnerRoute->PartnerUserRoute($user);
                DB::commit();

            }catch (\Exception $e){
                DB::rollBack();
                return $this->ajaxError($e->getMessage());
            }catch (\PDOException $e){
                DB::rollBack();
                return $this->ajaxError($e->getMessage());
            }

            $token = md5($this->tokenKey.$user['username']);
            Cache::store('redis')->set($token,json_encode($user),3*60*60);
            return $this->ajaxSuccess($user['username']);

        }else{
            return $this->ajaxError(collect($validator->messages())->implode('0','|'));
        }
    }


    public function code(Request $request){

        $toEmail = $this->decryptData($request->post('username'));
        $code = mt_rand(0,999999);
        $bool = SmsServer::sendSingleEmail(0,$toEmail,"yida@email.martianmine.com",true,"1297974144@qq.com","伽马超算","【伽马超算】您好，您的验证码是".$code."，请勿泄露。","伽马超算邮箱验证码","");
        Cache::store('redis')->set('email'.$toEmail,$code,5*60);
        if($bool){
            return $this->ajaxSuccess([]);
        }else{
            return $this->ajaxError();
        }

    }

}
